
*********************************************************************************
** PRELIMINARIES
*********************************************************************************

********************************************************************
** WAVE 3 POST-PLANTING DATA
********************************************************************

use "$dir/rawdata/LSMS Nigeria/2015/sect1_plantingw3.dta", clear // ROSTER
count
local samplesize=`r(N)'
merge 1:1 hhid indiv using "$dir/rawdata/LSMS Nigeria/2015/sect3_plantingw3.dta", assert(master matched) // LABOR

** Still in HH
gen w3p_inhh=s1q4!=2

** Household size
gen temp=1 if w3p_inhh==1
bys hhid: egen w3p_hh_size=total(temp)
drop temp

** Number of children 12 or younger
gen temp=1 if s1q6<=12 & w3p_inhh==1
bys hhid: egen w3p_hh_under12=total(temp)
drop temp

** Number of youth 18 or younger
gen temp=1 if s1q6<=18 & w3p_inhh==1
bys hhid: egen w3p_hh_under18=total(temp)
drop temp

** Number of adults 60 or older
gen temp=1 if s1q6>=60 & !missing(s1q6) & s1q6!=999 & w3p_inhh==1
bys hhid: egen w3p_hh_over60=total(temp)
drop temp

** Adults 12 to 60
gen temp=1 if s1q6>12 & s1q6<60 & !missing(s1q6) & s1q6!=999 & w3p_inhh==1
bys hhid: egen w3p_hh_12to60=total(temp)
drop temp

** Marital status
gen w3p_married=(s1q8==1 | s1q8==2) if !missing(s1q8)
gen w3p_married_monog=(s1q8==1) if !missing(s1q8)
gen w3p_married_polyg=(s1q8==2) if !missing(s1q8)

** Female
gen w3p_female=(s1q2==2) if !missing(s1q2)

** Age
gen w3p_age=s1q6
replace w3p_age=. if w3p_age==999

** Age of spouse
sum indiv 
forvalues x=1/`r(max)' {
	gen age_person`x'_temp=s1q6 if indiv==`x' & !missing(s1q6)
	bys hhid: egen age_person`x'=max(age_person`x'_temp)
	drop age_person`x'_temp
}

** Average age of others in the household
sum indiv 
forvalues x=1/`r(max)' {
	replace age_person`x'=. if indiv==`x'
}
egen w3p_hh_avgage=rowmean(age_person*)

** Spouse age
sum indiv
forvalues x=1/`r(max)' {
	replace age_person`x'=. if s1q13!=`x'
}
egen w3p_spouse_age=rowmean(age_person*)

** Age gap
gen w3p_spouse_agegap=w3p_age-w3p_spouse_age

** Birthday
gen w3p_birthday=mdy(s1q7_month,s1q7_day,s1q7_year)
format w3p_birthday %td

** Birth year
gen w3p_birthyear=s1q7_year
recode w3p_birthyear 9999=. 9998=.
replace w3p_birthyear=2015-w3p_age if missing(w3p_birthyear)

** Worked for a non-HH member
gen w3p_work_outsideHH=s3q4==1 if !missing(s3q4)
gen w3p_work_outsideHH_hrs=s3q4b
replace w3p_work_outsideHH_hrs=0 if w3p_work_outsideHH==0

** Worked in agriculture for a HH member
gen w3p_work_agHH=s3q5==1 if !missing(s3q5)
gen w3p_work_agHH_hrs=s3q5b
replace w3p_work_agHH_hrs=0 if w3p_work_agHH==0

** Worked in HH business
gen w3p_work_bizHH=s3q6==1 if !missing(s3q6)
gen w3p_work_bizHH_hrs=s3q6b
replace w3p_work_bizHH_hrs=0 if w3p_work_bizHH==0

** Any work in last 7 days (agriculture, for non-HH member, own account/business)
gen w3p_worked_last7days=0
replace w3p_worked_last7days=1 if s3q4==1
replace w3p_worked_last7days=1 if s3q5==1 
replace w3p_worked_last7days=1 if s3q6==1
replace w3p_worked_last7days=. if missing(s3q4) & missing(s3q5) & missing(s3q6)

** Hours worked in last 7 days
egen w3p_work_hrs=rowtotal(w3p_work_outsideHH_hrs w3p_work_agHH_hrs w3p_work_bizHH_hrs)
replace w3p_work_hrs=. if missing(w3p_work_outsideHH_hrs) & missing(w3p_work_agHH_hrs) & missing(w3p_work_bizHH_hrs)

** Works in agriculture
gen w3p_work_agri=(w3p_work_agHH==1 | s3q14==1 | s3q27==1) if (!missing(w3p_work_agHH) | !missing(s3q14) | !missing(s3q27))

** Industry of occupation
gen w3p_occ=s3q14
label values w3p_occ s3q14

** Highest education of biological father
gen w3p_father_educ=s1q22
label values w3p_father_educ s1q22
replace w3p_father_educ=. if w3p_father_educ==98

** Industry of occupation of biological father
gen w3p_father_occ=s1q23
label values w3p_father_occ S1Q23

** Highest education of biological mother
gen w3p_mother_educ=s1q27
label values w3p_mother_educ s1q27
replace w3p_mother_educ=. if w3p_mother_educ==98

** Industry of occupation of biological mother
gen w3p_mother_occ=s1q28
label values w3p_mother_occ s1q28

** ID of father and mother (for linking)
gen w3p_father_id=s1q20 
gen w3p_mother_id=s1q25

** GEOGRAPHIC INFORMATION
gen w3p_geo_urban=(sector==1) if !missing(sector)
gen w3p_geo_state=state
gen w3p_geo_lga=lga
replace w3p_geo_lga=s1q32b if !missing(s1q32b) // LGA code for where the HH member moved
label var w3p_geo_urban "Urban residence"
label var w3p_geo_state "State of residence"
label var w3p_geo_lga "LGA of residence"

** HH Consumption

merge m:1 hhid using "$dir/rawdata/LSMS Nigeria/2015/cons_agg_wave3_visit1.dta", assert(master matched) nogen

gen w3p_urban_fromcons=rururb==1 if !missing(rururb)
order w3p_urban, after(rururb)
label var w3p_urban "Urban residence"

ren totcons w3p_conspc

ren hhsize w3p_hhsize_fromcons

keep hhid indiv w3*
order hhid indiv w3p_geo*
save "$dir/data/NGA_w3p_individual.dta", replace
